package com.project.service.impl;

import com.project.bean.LogBean;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalTime;
import java.util.List;
@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper mapper;

    /**
     * 登陆同时添加日志信息
     * @param userName 用户名
     * @param pwd 密码
     * @return
     */
    @Override
    public UserBean login(String userName, String pwd) {
        UserBean userBean = mapper.login(userName,pwd);
        if (userBean == null){
            return null;
        }
        //日志内容
        String content = "用户"+userBean.getUserName()+"在"+
                LocalTime.now().toString().substring(0,5) +"登录系统";
        mapper.addLog(new LogBean(content));

        return userBean;
    }

    @Override
    public void updatePwd(Integer userId, String newPwd) {
        mapper.updatePwd(userId,newPwd);

    }

    @Override
    public List<UserBean> findByGrade(String grade) {
        return mapper.findByGrade(grade);
    }

    @Override
    public UserBean findById(Integer id) {
        return mapper.findById(id);
    }

    @Override
    public void updateGrade(Integer id, String newGrade) {
            mapper.updateGrade(id,newGrade);
    }

    @Override
    public void add(UserBean user) {

        mapper.add(user);
    }

    @Override
    public void del(Integer id) {

        mapper.del(id);
    }
}
